package com.zwz.mall.header;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.InternalAbstract;
import com.zwz.mall.R;
import com.zwz.mall.app.BaseApplication;
import com.zwz.mall.utils.DensityUtil;

/**
 * Created by Administrator on 2019/9/8 0008.
 */

public class LoadingHeader extends InternalAbstract implements RefreshHeader
{

    private ImageView mProgressView;//刷新动画视图
    private AnimationDrawable mProgressDrawable;//刷新动画

    public LoadingHeader(Context context) {
        this(context, null);
        initView(context);
    }

    public LoadingHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.initView(context);
    }

    public LoadingHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        setGravity(Gravity.CENTER);
        mProgressDrawable = new AnimationDrawable();
        //准备好资源图片
//        int[] ids = {R.mipmap.ic_baodian_downloading1,R.mipmap.ic_baodian_downloading2, R.mipmap.ic_baodian_downloading3, R.mipmap.ic_baodian_downloading4, R.mipmap.ic_baodian_downloading5,
//                R.mipmap.ic_baodian_downloading6, R.mipmap.ic_baodian_downloading7, R.mipmap.ic_baodian_downloading8, R.mipmap.ic_baodian_downloading9, R.mipmap.ic_baodian_downloading10, R.mipmap.ic_baodian_downloading11,
//                R.mipmap.ic_baodian_downloading12,};
//        //通过for循环添加每一帧动画
//        for (int i = 0; i < ids.length; i++) {
//            Drawable frame = getResources().getDrawable(ids[i]);
//            //设定时长
//            mProgressDrawable.addFrame(frame, 50);
//        }
        mProgressDrawable=(AnimationDrawable) ContextCompat.getDrawable(context, R.drawable.anim_loading);
        mProgressView = new ImageView(context);
        mProgressView.setImageDrawable(mProgressDrawable);

        addView(mProgressView, DensityUtil.dp2px(BaseApplication.getApplication(), 100), DensityUtil.dp2px(
                BaseApplication.getApplication(), 100));
        setMinimumHeight(DensityUtil.dp2px(BaseApplication.getApplication(), 60));
    }

    @Override
    @NonNull
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
        mProgressDrawable.start();//开始动画
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        mProgressDrawable.stop();//停止动画
        return 500;//延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
            case Refreshing:
                mProgressView.setVisibility(VISIBLE);//隐藏动画
                break;
            case ReleaseToRefresh:
                break;
        }
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
    }
}

